package com.siegelion.section.my;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.siegelion.R;
import com.siegelion.plugin.MethodChannelPlugin;
import com.siegelion.plugin.MethodChannelTool;
import com.siegelion.section.base.BaseInitFragment;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.facade.Flutter;
import io.flutter.view.FlutterView;

public class MyFragment extends BaseInitFragment {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        FlutterEngine myFlutterEngine = new FlutterEngine(getContext());
//        //设置一些参数
//        myFlutterEngine.getNavigationChannel().setInitialRoute("my");
//        //设置缓存
//        myFlutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
//        FlutterEngineCache.getInstance().put("my", myFlutterEngine);
//        flutterView=findViewById(R.id.flutter_view);
//        FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get("my");
//        flutterView.attachToFlutterEngine(flutterEngine);
//        //MethodChannelPlugin.registerWith(flutterView);
//       // MethodChannelTool.registerWith(flutterEngine,getActivity());
//    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       FlutterView flutterView = Flutter.createView(getActivity(),getLifecycle(),"my");
        MethodChannelPlugin.registerWith(flutterView);
        return flutterView;
//        //创建引擎
//        FlutterEngine lFlutterEngine = new FlutterEngine(getContext());
//        //设置一些参数
//        lFlutterEngine.getNavigationChannel().setInitialRoute("my");
//        //设置消息通道
//        //setMessageChannel(lFlutterEngine);
//        //设置缓存
//        lFlutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
//        FlutterEngineCache.getInstance().put("test_engine", lFlutterEngine);
//        startActivity(FlutterActivity
//                .withNewEngine()
//                .initialRoute("/test")
//                .build(getContext()));
//        FlutterFragment lFlutterFragment = FlutterFragment
//                .withCachedEngine("test_engine")
//                .build();
//
//        lFlutterFragment.onCreateView(inflater,container,savedInstanceState);
//        return lFlutterFragment.getView();
    }
}
